﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
using System.Net;
using System.IO;
namespace ProcessesSMS
{
    /// <summary>
    /// Summary description for CKVN.
    /// </summary>
    public class NAVIS
    {
    
        private SMS_MO WMO;
        private SMS_MT WMT;
        //private SMS_LIST WML;
        ArrayList arr = new ArrayList();
        public NAVIS(SMS_MO MO)
        {
            WMO = MO;
        }

        public NAVIS()
        {
        }
        public void run()
        {
            WMT = new SMS_MT(WMO);
            //string tmpMsg = WMO.Message.Substring(WMO.CommandCode.Length).Trim().Replace("  ", " ").Replace("  ", " ");
            //tmpMsg = tmpMsg.Replace("  ", " ").Trim().ToUpper();
            //string[] msg = tmpMsg.Split(new char[] { ' ' });
            string[] strReturnMsg = GetReturnMsg().Split('|');
            if (strReturnMsg.Length > 0)
            {
                for (int i = 0; i < strReturnMsg.Length; i++)
                {
                    WMT.AddMessage(WMO.UserID, strReturnMsg[i], SMS_ContentType.SMS_TEXT);
                    WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
                }
                WMT.Insert();
            }
            return;
        }
        private string GetReturnMsg()
        {
            string[] msg = WMO.Message.Trim().Replace("  ", " ").Split(' ');
            string msgBody = string.Empty;
            if ((msg.Length == 2) && ((WMO.UserID == "84902222776") || (WMO.ServiceID.Replace("04", "") == "6772") || (WMO.ServiceID.Replace("04", "") == "6672") || (WMO.ServiceID.Replace("04", "") == "6572")))
            {
                msgBody = GetMsgStockFromURL("http://www.6x72.vn/SMSNavis/?c=" + msg[0] + "&d=" + msg[1]);
            }
            else
            {
                msgBody = "Cu phap khong hop le!Cac cp dung: CT 01, CT dd/mm/yy,CT mm/dd, CT MaCK, CP 01, CP 02, ND 01, KN MUA, TV CK, CL V, CL 01, CL 02, TV V. Ap dung cho dau so 6772";
            }
            return msgBody;
        }
        //GetDataFromURL
        private string GetMsgStockFromURL(string url)
        {
            string strval = "";
            WebRequest WQob = WebRequest.Create(url);
            WebResponse WPob = WQob.GetResponse();
            StreamReader SRob = new StreamReader(WPob.GetResponseStream());
            strval = SRob.ReadToEnd();
            WQob.Abort();
            return strval;
        }
    }
}
